﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using YourWorld.Common;
using System.Collections.ObjectModel;
using BusinessObjects;
using Microsoft.Practices.Composite.Presentation.Commands;
using YourWorld.Common.Infrastucture.Events;
using System.Collections.Specialized;

namespace MainGameModule.ViewModels
{
    public class BinViewModel : ViewModelBase
    {
        private int materialId;
        private string name;
        private ObservableCollection<Rubbish> contentItems;
        private int contentWidth;
        private string color;
        private string secondColor;
        private string image;
        private double fillPercent = 0.1;
        private string description;

        private const int Step = 10;
        
        public BinViewModel()
        {
            this.SelectBinCommand = new DelegateCommand<object>( this.BinSelected );
        }

        public String Name
        {
            get
            {
                return this.name;
            }
            set
            {
                if ( this.name == value )
                    return;

                this.name = value;
                this.NotifyPropertyChanged( "Name" );
            }
        }

        public int MaterialId
        {
            get
            {
                return this.materialId;
            }
            set
            {
                if ( this.materialId == value )
                    return;

                this.materialId = value;
                this.NotifyPropertyChanged( "MaterialId" );
            }
        }

        public int ContentWidth
        {
            get
            {
                return this.contentWidth;
            }
            set
            {
                if ( this.contentWidth == value )
                    return;

                this.contentWidth = value;
                this.NotifyPropertyChanged( "ContentWidth" );
            }
        }

        public string Color
        {
            get
            {
                return this.color;
            }
            set
            {
                if ( this.color == value )
                    return;

                this.color = value;
                this.NotifyPropertyChanged( "Color" );
            }
        }

        public string Description
        {
            get
            {
                return this.description;
            }
            set
            {
                if ( this.description == value )
                    return;

                this.description = value;
                this.NotifyPropertyChanged( "Description" );
            }
        }

        public string SecondColor
        {
            get
            {
                return this.secondColor;
            }
            set
            {
                if ( this.secondColor == value )
                    return;

                this.secondColor = value;
                this.NotifyPropertyChanged( "SecondColor" );
            }
        }
        
        public string Image
        {
            get
            {
                return this.image;
            }
            set
            {
                if ( this.image == value )
                    return;

                this.image = value;
                this.NotifyPropertyChanged( "Image" );
            }
        }

        public double FillPercent
        {
            get
            {
                return this.fillPercent;
            }
            set
            {
                if ( this.fillPercent == value )
                    return;

                this.fillPercent = value;
                this.NotifyPropertyChanged( "FillPercent" );
            }
        }

        public ObservableCollection<Rubbish> ContentItems
        {
            get
            {
                if ( this.contentItems == null )
                    this.contentItems = new ObservableCollection<Rubbish>();

                return this.contentItems;
            }
            set
            {
                if ( this.contentItems == value )
                    return;

                if ( this.contentItems != null )
                    this.contentItems.CollectionChanged -= new NotifyCollectionChangedEventHandler( this.ContentItems_CollectionChanged );

                this.contentItems = value;
                this.contentItems.CollectionChanged += new NotifyCollectionChangedEventHandler( this.ContentItems_CollectionChanged );

                this.NotifyPropertyChanged( "ContentItems" );
            }
        }

        private void ContentItems_CollectionChanged( object sender, NotifyCollectionChangedEventArgs e )
        {
            this.ContentWidth = this.ContentItems.Count * Step;
        }

        public ICommand SelectBinCommand
        {
            get;
            private set;
        }

        private void BinSelected( object o )
        {
            EventService.Aggregator.GetEvent<ChooseBinEvent>().Publish( this.MaterialId );
        }
    }
}
